package MOLE;

import java.util.*;

/** 
 SQL_MY ��ժҪ˵����
*/
public class SQLOS
{
	public static String connstr = MOLE.SQL.connectionString;
	private String Table = "";
	 private String InserCol = "";
	private String InserValue = "";
	private String UpdateCol = "";
	private ArrayList SqlParas = new ArrayList();

	  public String _StoreSql = "";



	public String InsertSql = "";
	public String UpdateSql = "";



	public SQLOS(String tableName)
	{
		 Table = tableName;
		_StoreSql = " exec " + tableName + "  ";
	}

	public SQLOS(String StoreProcessNameStr, String demo)
	{
		_StoreSql = " exec " + StoreProcessNameStr + "  ";

	}

	public final void RS(String columename, Object o)
	{
		InserCol += ",[" + columename + "]";
		InserValue += ",@" + columename;
		InserCol = tangible.DotNetToJavaStringHelper.trim(InserCol, ',');
		InserValue = tangible.DotNetToJavaStringHelper.trim(InserValue, ',');
		InsertSql = GetInsertSql();
		 //upcols+=columename+"=@"+columename+",";
		UpdateCol += ",[" + columename + "]=@" + columename;
		UpdateCol = tangible.DotNetToJavaStringHelper.trim(UpdateCol, ',');
	//	UpdateSql=GetUpdateSql();

		SqlParas.add(new SqlParameter("@" + columename,o));
	}


	//exec store process
	public final void RS(Object storeProcessParameterVale)
	{
		Object o = storeProcessParameterVale;
		if (o.getClass().toString().equals("System.Int32") || o.getClass().toString().equals("System.Decimal") || o.getClass().toString().equals("System.Double") || o.getClass().toString().equals("System.Int64"))
		{
			_StoreSql += o + ",";
		}

		else //System.String
		{
			_StoreSql += "'" + o.toString().replace("'","''") + "'" + ",";
		}
	//	StoreSql=StoreSql.Trim(',');

	}

	public final DataTable ExecStoreProcess()
	{
		_StoreSql = tangible.DotNetToJavaStringHelper.trim(_StoreSql, ',');
		DataTable d = MOLE.SQL.GetDataTable(_StoreSql);
		return d;

	}

	public final String ExecStoreProcess(int ReturnStrOrInt)
	{
		 DataTable d = ExecStoreProcess();

		 return d.Rows[0]["result"].toString().trim();
	}




   private String GetInsertSql()
   {

		String s = " INSERT INTO  @table (@InserCol) VALUES (@InserValue)  ";
		s = s.replace("@table",Table);
		s = s.replace("@InserCol",InserCol);
		s = s.replace("@InserValue",InserValue);
		 return s;
   }


	public final String GetUpdateSql(String key, int keyvalue)
	{
		  //UPDATE AgentInfo SET AIKey = ' xxx'
		String s = " UPDATE  @table Set @upcol where  @key=@value";
		s = s.replace("@table",Table);
		s = s.replace("@upcol",UpdateCol);
		s = s.replace("@key",key);
		s = s.replace("@value",String.valueOf(keyvalue));

		return s + "";
	}


	public final String GetQuerySql()
	{
		String s = " INSERT INTO " + Table + ") VALUES (" + InserValue + ")";
		return s;
	}



	public final ArrayList InserTrans()
	{
	 //	SqlParameter[] paras= (SqlParameter[])((ICloneable)SqlParas.ToArray()).Clone();

		SqlParameter[] clonedParms = new SqlParameter[SqlParas.size()];
		for (int i = 0 ; i < SqlParas.size();i++)
		{
			clonedParms[i] = (SqlParameter)((Cloneable)SqlParas.get(i)).Clone();
		}

		 Hashtable h = new Hashtable();
		 h.put(InsertSql,clonedParms);


		ArrayList a = new ArrayList();
		a.add(h);
		return a;
	}





	public final String GetDeleteSql()
	{
		String s = " INSERT INTO " + Table + ") VALUES (" + InserValue + ")";
		return s;
	}


	public final void Insert()
	{
		SqlConnection conn = null;
		SqlCommand cmd = null;
		try
		{
			conn = new SqlConnection(connstr);
			conn.Open();


			cmd = new SqlCommand(InsertSql,conn);
			for (int n = 0;n < SqlParas.size();n++)
			{
				cmd.Parameters.Add(SqlParas.get(n));

			}
			cmd.ExecuteNonQuery();
		}
		 finally
		 {
			cmd.Parameters.Clear();
			 cmd.Dispose();
			conn.Close();
			conn.Dispose();
		 }
	}

	public final void Update(String primaryKey, int primaryKeyValue)
	{
		SqlConnection conn = new SqlConnection(connstr);
		conn.Open();

		 String sql = GetUpdateSql(primaryKey, primaryKeyValue);

		SqlCommand cmd = new SqlCommand(sql,conn);
		for (int n = 0;n < SqlParas.size();n++)
		{
			cmd.Parameters.Add(SqlParas.get(n));

		}
		cmd.ExecuteNonQuery();
		//cmd.ExecuteNonQuery();
		cmd.Parameters.Clear();

		cmd.Dispose();
		conn.Close();
		conn.Dispose();
		//System.Web.HttpContext.Current.Response.Write(sqlParas[0].GetType().ToString());
	}

	public final Hashtable UpdateTrans(String primaryKey, int primaryKeyValue)
	{
		//	SqlParameter[] paras= (SqlParameter[])((ICloneable)SqlParas.ToArray()).Clone();
		String sql = GetUpdateSql(primaryKey, primaryKeyValue);
		SqlParameter[] clonedParms = new SqlParameter[SqlParas.size()];
		for (int i = 0 ; i < SqlParas.size();i++)
		{
			clonedParms[i] = (SqlParameter)((Cloneable)SqlParas.get(i)).Clone();
		}

		Hashtable h = new Hashtable();
		h.put(sql,clonedParms);
		return h;
	}

	public final ArrayList _UpdateTrans(String primaryKey, int primaryKeyValue)
	{
		ArrayList a = new ArrayList();
		Hashtable h = UpdateTrans(primaryKey, primaryKeyValue);
		a.add(h);
		return a;

	}





	public final boolean _Exists(String whereString)
	{
		 DataTable dt = _List(whereString);
		if (dt.Rows.size() == 0)
		{
			return false;
		}
		else
		{
			return true;
		}
	}

	public final DataTable _List()
	{
		return _List("");
	}

	public final DataTable _List(String whereString)
	{
		return _List(whereString, "");
	}
	public final DataTable _List(String whereString, String orderString)
	{

		String sql = "";
		if (whereString.equals(""))
		{
			sql = "select * from " + Table;
		}
		else
		{
			sql = "select * from " + Table + "  where  " + whereString;
		}

		if (orderString.equals(""))
		{
		}
		else
		{
			sql = sql + " order by " + orderString;
		}
		DataTable dt = MOLE.SQL.GetDataTable(sql);
		return dt;

	}



	public final String _GetFieldVale(String field, String whereString)
	{
		 DataTable dt = _List(whereString);
		if (dt.Rows.size() != 0)
		{
			return dt.Rows[0][field].toString();
		}
		 return "";


	}

	public static String _GetFieldValue(String fieldName, String sql)
	{
		DataTable d = MOLE.SQL.GetDataTable(sql);
		 if (d.Rows.size() == 0)
		 {
			 return "";
		 }
		else
		{
			 return d.Rows[0][fieldName].toString();
		}
	}









	//bind datagrid
	public static void BindGrid(String sql, System.Web.UI.WebControls.DataGrid dg)
	{
		DataTable d = SQL.GetDataTable(sql);
		dg.DataSource = d;
		dg.DataBind();
	}
	 public static void BindGrid(DataTable d, System.Web.UI.WebControls.DataGrid dg)
	 {
		 dg.DataSource = d;
		dg.DataBind();
	 }



//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
		///#region no resont to use this method .


	public final int GetFieldInt(String sql, String field)
	{
		String s = sql;
		int i = 0;
		DataTable dt = MOLE.SQL.GetDataTable(s);
		try
		{
			i = Integer.parseInt(dt.Rows[0][field].toString().trim());
		}
		catch (java.lang.Exception e)
		{
		}
		return i;
	}

	public final boolean Exists(String key, int keyvalue)
	{
		String s = "select * from " + Table + " where " + key + "=" + String.valueOf(keyvalue);
		DataTable dt = MOLE.SQL.GetDataTable(s);
		if (dt.Rows.size() == 0)
		{
			return false;
		}
		else
		{
			return true;
		}
	}

	public final DataTable GetDataTable(String key, int keyvalue)
	{
		String s = "select * from " + Table + " where " + key + "=" + String.valueOf(keyvalue);
		DataTable dt = MOLE.SQL.GetDataTable(s);
		return dt;
	}

	public final DataTable GetDataTable(int count)
	{
		String s = "select top " + count + " * from " + Table;
		DataTable dt = MOLE.SQL.GetDataTable(s);
		return dt;
	}

//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
		///#endregion


}